﻿using System;
using System.Drawing;
using System.Windows.Forms;
using HomeDMSDomain;

namespace HomeDms
{
    public partial class PageControl : UserControl
    {
        public PageControl()
        {
            InitializeComponent();
            _page = new Page();
        }
        
        public PageControl(string pageName, Image pageImage)
            : this ()
        {
            DefaultPageName = pageName;
            PageImage = pageImage;
        }

        public PageControl(string pageName,byte[] file)
        {
            DefaultPageName = pageName;
            _page.File = file;
        }

        public PageControl(Page page)
            : this()
        {
            DefaultPageName = page.Name;
            PageImage = page.Image;
            this.Page = page;
        }

        private Page _page;

        public Page Page
        {
            get {
                if (!string.IsNullOrEmpty(textBox1.Text))
                {
                    _page.Name = textBox1.Text;
                }
                else
                {
                    _page.Name = this.DefaultPageName;
                }
                return _page; 
            }
            set { 
                _page = value;
                if (!string.IsNullOrEmpty(_page.Name))
                {
                    textBox1.Text = _page.Name;
                }
            }
        }

        
        public int PageOrder
        {
            get 
            { 
                return Page.Order; 
            }
            set { Page.Order = value; }
        }


        string _defaultPageName;
        Image _pageImage;
        
        public Image PageImage
        {
            get 
            { 
                return Page.Image; 
            }
            set 
            { 
                Page.Image = value;
                pictureBox1.OriginalImage = Page.Image;
            }
        }

        public new string Text
        {
            get
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                {
                    return this.DefaultPageName;
                }
                else
                {
                    return textBox1.Text;
                }
            }
        }

        public bool PageSelected
        {
            get
            {
                return checkBox1.Checked;
            }
        }

        public string DefaultPageName
        {
            get
            {
                return _defaultPageName;
            }
            set
            {
                _defaultPageName = value;
                textBox1.Text = _defaultPageName;
            }
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {            
            BeginEdit();
        }

        bool _editing = false;

        private void BeginEdit()
        {
            if (_editing)
                return;
            textBox1.ReadOnly = false;
            textBox1.BorderStyle = BorderStyle.FixedSingle;
            textBox1.SelectAll();
            SetHeight();
            BackColor = SystemColors.Highlight;
            _editing = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            SetHeight();
            SetTextAlign();
        }

        private void SetHeight()
        {
            int lineCount = textBox1.GetLineFromCharIndex(textBox1.Text.Length - 1) + 1;
            textBox1.Height = lineCount * 17;
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            EndEdit();
        }

        public bool UserAlteredName
        {
            get
            {
                if (textBox1.Text == "")
                    return false;
                string[] s = textBox1.Text.Split(' ');
                int dummy;
                if (s.Length == 2 && s[0] == "Page" && int.TryParse(s[1], out dummy))
                    return false;
                return true;
            }
        }

        public event EventHandler Changed;

        private void EndEdit()
        {
            if (!_editing)
                return;
            textBox1.ReadOnly = true;
            if (textBox1.Text == "")
                textBox1.Text = _defaultPageName;
            SetTextAlign();
            textBox1.BorderStyle = BorderStyle.None;
            textBox1.Height = 17;
            BackColor = SystemColors.Control;
            _editing = false;
            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        private void SetTextAlign()
        {
            if (textBox1.Text != _defaultPageName && textBox1.Text != "")
                textBox1.TextAlign = HorizontalAlignment.Left;
            else
                textBox1.TextAlign = HorizontalAlignment.Center;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
                BeginEdit();
            if (e.KeyCode == Keys.Enter)
                EndEdit();
            e.Handled = true;
        }

        private void PageControl_MouseEnter(object sender, EventArgs e)
        {
            BackColor = SystemColors.Highlight;
            //textBox1.BorderStyle = BorderStyle.FixedSingle;
        }

        private void PageControl_MouseLeave(object sender, EventArgs e)
        {
            if (!_editing)
                BackColor = SystemColors.Control;
            //textBox1.BorderStyle = BorderStyle.None;
        }

        public static PageZoom pageZoomForm = null;

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Edit();
        }

        public void Edit()
        {
            if (pageZoomForm == null)
                pageZoomForm = new PageZoom();
            pageZoomForm.ImageChanged = new EventHandler(pageZoomForm_ImageChanged); // Don't use += !!!
            pageZoomForm.Image = this.pictureBox1.OriginalImage;
            if (!pageZoomForm.Visible)
                pageZoomForm.Show(this);
        }

        void pageZoomForm_ImageChanged(object sender, EventArgs e)
        {
            PageImage = pageZoomForm.Image;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (pageZoomForm == null)
                return;
            if (pageZoomForm.Visible)
            {
                pageZoomForm.Image = this.pictureBox1.OriginalImage;
                pageZoomForm.ImageChanged = new EventHandler(pageZoomForm_ImageChanged); // Don't use += !!!
            }
        }
    }
}
